package com.mogujie.tt.utils;

/**
 * Created by user on 2015/10/29.
 */

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.mogujie.tt.DB.sp.LoginSp;
import com.mogujie.tt.DB.sp.RegisterSp;
import com.mogujie.tt.config.RegisterConstant;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


public class   SubmitUserDetailsTask  extends AsyncTask<Void,Void,String> {

    Context context;
    JSONObject request;
    JSONObject rtjson;//json对象
    String newMessageInfo = "";
    public SubmitUserDetailsTask(Context context) {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(Void... params) {
        Map<String, Object> parms = new HashMap<String ,Object>();
        parms.put("id", LoginSp.instance().getLoginIdentity().getLoginId());
        parms.put("phone", RegisterSp.instance().getSpDetailedInfo().getDetailedMobilePhone().toString().trim());
        parms.put("sex",RegisterSp.instance().getSpDetailedInfo().getDetailedSex());
        parms.put("nick",RegisterSp.instance().getSpDetailedInfo().getDetailedNickName().toString().trim());
        parms.put("email",RegisterSp.instance().getSpDetailedInfo().getDetailedEmail().toString().trim());
        //    parms.put("avatar",RegisterSp.instance().getSpDetailedInfo().getDetailedHeadPortrait().toString().trim());
        parms.put("avatar","");
        request = new JSONObject();//新建json格式
        request = JsonUtil.jsonRequest(RegisterConstant.SERVER_NAME, RegisterConstant.SUBMIT_USER_DETAILED, parms);
        String jsonstr = HttpUtilsApache.usHttpPostJson(RegisterConstant.SERVER_PATH, request);
        JsonUtil jsonUtil=new JsonUtil();//建立json验证
        if(jsonstr.length()>3){
            rtjson=jsonUtil.getOutputJson(jsonstr);
            Log.v("", "rtjson = " + rtjson);
        }else{
            newMessageInfo=HttpUtilsApache.errorMsg(jsonstr);//打印出错信息
        }
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        if("".equals(newMessageInfo) || newMessageInfo==null)
        {
            if(rtjson!=null && !"".equals(rtjson)){

                try {
                    String responseCode=rtjson.getString("response_code").toString();
                    if("1".equals(responseCode)) {
                        //    triggerEvent(ConcurrentUserDetailsEvent.ConcurrentNicknameEvent.MODIFY_NICKNAME);
                        //    Toast.makeText(context,rtjson.getString("response_desc").toString(),Toast.LENGTH_LONG).show();
                        // Toast.makeText(context,"用户修改详细信息成功",Toast.LENGTH_LONG).show();
                    }else if("-1".equals(responseCode)) {
                        Toast.makeText(context, rtjson.getString("response_desc").toString(), Toast.LENGTH_LONG).show();
                    }

                } catch (Exception e) {}
            }
        }
        else {
            Toast.makeText(context, newMessageInfo, Toast.LENGTH_SHORT).show();
        }
    }
}

